var cityJS = require('../../../utils/city.js')
var config = require('../../../utils/config');
var util = require('../../../utils/util')
var API_URL = config.API_URL;
var id;
var app = getApp();
Page({
    data: {
        hidden: true,
        isDefault: false
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        console.log(options)
        var that = this;
        var url;
        id = options.id;
        console.log(id);
        // 初始化
        cityJS.init(that);
        if (id != 0 ? true : false) {
            var address = app.globalData.address;
            that.setData(address)
        }
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    choosearea: function () {
       
        this.setData({
            isShow: true
        })
    },
    mask: function () {
        this.setData({
            isShow: false
        })
    },
    bindChange: function (e) {
        const current_value = e.detail.value;
        var data = this.data;
        cityJS.change(current_value, this);
        this.setData({
          region: data.provice + data.city + data.district
        })
    },
    checktel: function (e) {
        var tel = e.detail.value;
        util.checkMobilePhone(tel);
    },
    save: function (e) {
        var input = e.detail.value;
        var url;
        console.log(input)
        if (input.consignee == '') {
            util.showNotice("请输入收货人姓名");
            return
        }
        if (!util.checkMobilePhone(input.phoneTel)) {
            util.showNotice("请输入正确的手机号码");
            return
        }
        if (input.zipCode.length != 6) {
            util.showNotice("请输入6位邮政编码");
            return
        }
        if (input.regionName == ''){
          util.showNotice("请选择地区");
          return
        }
        if (input.address == '') {
            util.showNotice("请输入详细收货地址");
            return
        }
        if (id != 0 ? true : false) {
            url = API_URL.edit_address;
        } else {
            url = API_URL.add_address;
        }
        var params = {
          consignee: input.consignee,
          region_name: input.regionName,
          mobile: input.phoneTel,
          zip: input.zipCode,
          address: input.address,
          address_id: id,
          default: input.isDefault?1:0
        }
        util.PRL(url,params)
        .then(res => {
          my.showToast({
         content: '保存成功',
            icon: 'success',
            duration: 800
          })
          setTimeout(function () {
            my.redirectTo({
              url: '/pages/address/address'
            })
          }, 800)
        }).catch(res => {
          console.log(res);
        })
    },
    switch:function(e){
      console.log(e);
      var isDefault = !this.data.isDefault;
      console.log(isDefault);
      this.setData({
        isDefault: isDefault
      })
    }
})